﻿<?php
@session_start();
include_once("../initial.php");
$DOCUMENT_ROOT=$_SERVER["DOCUMENT_ROOT"];
include_once(constant('PROJECT_DIR')."/logic/orderLogic.php");
include_once(constant('PROJECT_DIR')."/logic/courseLogic.php");
include_once(constant('PROJECT_DIR')."/logic/deviceLogic.php");
$LIST_NUMBER_PER_PAGE=10;
//begin to read class
require_once(constant('PROJECT_DIR')."/checkLogin.php");
require_once(constant('PROJECT_DIR')."/checkAdminLogin.php");

$adminId=$_SESSION['user'];
$uid=$adminId;
$ol = new orderLogic();
if(isset($uid) && $uid!="")
{
		$smarty->assign("userId", $uid);
		$smarty->assign("unReadNum", $unReadNum);
}

$action="";
if(isset($_POST['action']))
{
     $action = $_POST['action'];
}
else if(isset($_GET['action']))
{
     $action=$_GET['action'];
}
$searchResult=array();
$searchNote="";
$lastAction="";
if(isset($_GET['lastAction']))
{
		$lastAction=$_GET['lastAction'];
}
if($action == "pass")
{
     			$id=$_GET['id'];
     			$ol->passOrder($id, $adminId);
}
if($action == "reject")
{
     			$id=$_GET['id'];
     			$ol->rejectOrder($id, $adminId);
}
if($action == "checkout")
{
    		//借出
    		$id=$_GET['id'];
     		$ol->checkout($id);
}
if($action == "checkin")
{
    		//归还
    		$id=$_GET['id'];
     		$ol->checkin($id);
}

if($action == "search" || $lastAction=="search")
{
				$keyword = trim($_GET['keyword']);
				$endDate=$_GET['endDate'];
				$beginDate=$_GET['beginDate'];
				$type = $_GET['type'];
    		if(strlen($keyword)<3 && $endDate=='' && $beginDate=='')
    		{
    				$searchNote .="输入的关键词太短，不进行搜索,请输入长度超过3的字符串.";
    				$smarty->assign("searchNote", $searchNote);
    		}
    		//keyword长度要超过2
    		$searchResult = $ol->searchByKeyword($keyword, $beginDate, $endDate, $type, $adminId);//默认显示
    		if(count($searchResult)==30)
    		{
    				$searchNote .="搜索词得到太多结果，只显示30条，请输入更长的搜索词";
    				$smarty->assign("searchNote", $searchNote);
    		}
    		if(count($searchResult)==0)
    		{
    				$searchNote .="没有结果，展示所有结果";
    				$smarty->assign("searchNote", $searchNote);
    		}
    		$smarty->assign("action", "search");
    		$smarty->assign("keyword", $keyword);
    		$smarty->assign("beginDate", $beginDate);
    		$smarty->assign("endDate", $endDate);
    		$smarty->assign("type", $type);

}



//handling the page index
$ordersNumber=0;
if(count($searchResult)>0)
{
		$ordersNumber=count($searchResult);
}
else
{
	$ordersNumber=$ol->getOrdersNumberByAdminId($adminId);
}

$pageIndex=1;
$totalPageNumber=ceil($ordersNumber/$LIST_NUMBER_PER_PAGE);
if(isset($_GET['pageIndex']))
{
     $pageIndex=$_GET['pageIndex'];
     if($pageIndex<1 || $pageIndex > $totalPageNumber)
     {
         $pageIndex=1;
     }
}
$startItem=($pageIndex-1)*$LIST_NUMBER_PER_PAGE;
$ItemNumber=$LIST_NUMBER_PER_PAGE;

$leftPageIndex=$pageIndex<=1?1:$pageIndex-1;
$rightPageIndex=$pageIndex>=$totalPageNumber?$totalPageNumber:$pageIndex+1;
$leftMostPageIndex=$pageIndex<=5?1:$pageIndex-5;//左边显示5个页面索引
$rightMostPageIndex=$totalPageNumber<$pageIndex+5?$totalPageNumber:$pageIndex+5;//右边显示5个页面索引
$smarty->assign("leftMostPageIndex",$leftMostPageIndex);
$smarty->assign("rightMostPageIndex",$rightMostPageIndex);
$smarty->assign("leftPageIndex",$leftPageIndex);
$smarty->assign("rightPageIndex",$rightPageIndex);
$smarty->assign("pageIndex",$pageIndex);
$smarty->assign("ordersNumber", $ordersNumber);
$allorders=array();
if(count($searchResult)>0)
{
		$allorders=$searchResult;
}
else
{
		$allorders=$ol->getOrdersByAdminId($adminId, "limit $startItem,$ItemNumber");
}
$dh = new deviceLogic();
					for($i=0; $i<count($allorders);$i++)
					{
							$pictures=$dh->get_device_pictures($allorders[$i]['did']);
							if(count($pictures)>0)
							{
									$allorders[$i]['picture']=constant('PROJECT_URL')."/".constant('PICTURE_SUBDIR')."/".$pictures[0]['picture_name'];
							}
							else
							{
									$allorders[$i]['picture']=constant('PROJECT_URL')."/images/default.jpg";
							}
					}
//add the class info and the type
$smarty->assign("orders",$allorders);
//handling page Index end

//填充需要信息

$ordersnumber = $ol->getOrdersNumberByAdminId($adminId);
$smarty->assign("ordernumber", $ordersNumber);


$smarty->assign("menuSelected", "order");
$smarty->assign("projectUrl", constant('PROJECT_URL'));
$smarty->assign("currentUrl","admin/order.php");
$smarty->display("admin/order.html");
?>

